import { Injectable, OnInit } from '@angular/core';
import { environment } from '../../../environments/environment';

import { GoogleAnalyticsService } from 'googleanalytics-angular';
import { GoogleAnalyticsBeacon } from '../../models/googleAnalytics.beacon'
import { TrackingOptions } from 'googleanalytics-angular/lib/interfaces/tracking-options';

import { GAService } from '../services/ga.service'

@Injectable()
export class AnalyticsService {    
    constructor(private googleAnalyticsService: GoogleAnalyticsService, private gaService: GAService) { }

    public sendBeacon(beacon: GoogleAnalyticsBeacon) {
        this.gaService.connect().then(x=> {
            beacon.label.Date = new Date();
            beacon.label.AccountId = x.accountId;
            beacon.label.UserId = x.userId;
            
            var trackingId = environment.gaTrackingId;
            var options = {
                clientId: x.userId,
                cookieDomain: 'auto',
                name: 'globalAccess', 
                namedTracker: environment.production
            }
            
            this.googleAnalyticsService.configure(trackingId, options);
            this.googleAnalyticsService.set({"dimension1": x.userId});
            
            
            this.googleAnalyticsService.event.emit({
                category: beacon.category,
                action: beacon.action,
                label: JSON.stringify(beacon.label)
            });
        });
    }
}